package com.novax.ex.activity.provider.controller;

import cn.hutool.core.bean.BeanUtil;
import com.novax.ex.activity.infrastructure.entity.MallProductDescriptionLanguage;
import com.novax.ex.activity.open.api.inner.MallProductDescriptionLanguageInnerApi;
import com.novax.ex.activity.open.model.query.MallProductDescriptionLanguageQuery;
import com.novax.ex.activity.open.model.request.MallProductDescriptionLanguageRequest;
import com.novax.ex.activity.open.model.response.MallProductDescriptionLanguageResponse;
import com.novax.ex.activity.provider.service.MallProductDescriptionLanguageService;
import com.novax.ex.common.constant.InternationalCode;
import com.novax.ex.common.constant.StatusCode;
import com.novax.ex.common.results.ReturnResult;
import com.novax.ex.common.util.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Description:简介配置
 *
 * @author Deucalion@novax.email
 * @date 2023/2/21 17:21
 */
@Slf4j
@RestController
public class MallProductDescriptionLanguageController extends BaseActivityController implements MallProductDescriptionLanguageInnerApi {
    @Resource
    private MallProductDescriptionLanguageService mallProductDescriptionLanguageService;

    @Override
    public ReturnResult add(String language, Long adminId, MallProductDescriptionLanguageRequest request, BindingResult bindingResult) {
        // 缺少必填项
        if(bindingResult.hasErrors()){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }
        // 数据查重
        MallProductDescriptionLanguage repetitive = mallProductDescriptionLanguageService.findByLanguage(request.getMallProductId(),request.getLanguage());
        if(null != repetitive){
            return ReturnResult.fail(getInternational(language, InternationalCode.DUPLICATED_DATA));
        }
        MallProductDescriptionLanguage entity = BeanUtil.copyProperties(request, MallProductDescriptionLanguage.class);
        entity.setId(null);
        entity.setCreator(adminId);
        boolean result = mallProductDescriptionLanguageService.addSelective(entity);
        if (result) {
            return ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS));
        }
        return ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult modify(String language, Long adminId, MallProductDescriptionLanguageRequest request, BindingResult bindingResult) {
        // 缺少必填项
        if(null == request.getId()){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }
        if(bindingResult.hasErrors()){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }
        MallProductDescriptionLanguage old = mallProductDescriptionLanguageService.find(request.getId());
        if(null == old){
            return ReturnResult.fail(getInternational(language, InternationalCode.NOT_EXIST));
        }
        // 数据查重
        MallProductDescriptionLanguage repetitive = mallProductDescriptionLanguageService.findByLanguage(request.getMallProductId(),request.getLanguage());
        if(null != repetitive && !repetitive.getId().equals(old.getId())){
            return ReturnResult.fail(getInternational(language, InternationalCode.DUPLICATED_DATA));
        }
        MallProductDescriptionLanguage entity = BeanUtil.copyProperties(request, MallProductDescriptionLanguage.class);
        entity.setUpdater(adminId);
        boolean result = mallProductDescriptionLanguageService.modifySelective(entity);
        if (result) {
            return ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS));
        }
        return ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult remove(Long id, String language) {
        MallProductDescriptionLanguage old = mallProductDescriptionLanguageService.find(id);
        if(null == old){
            return ReturnResult.fail(getInternational(language, InternationalCode.NOT_EXIST));
        }
        boolean result = mallProductDescriptionLanguageService.remove(id);
        if (result) {
            return ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS));
        }
        return ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult<PageUtil<MallProductDescriptionLanguageResponse>> list(String language, MallProductDescriptionLanguageQuery query) {
        PageUtil<MallProductDescriptionLanguageResponse> pages = new PageUtil<>(query);
        Map<String, Object> condition = new HashMap<>();
        condition.put("query", query);
        pages.setCondition(condition);
        mallProductDescriptionLanguageService.findPage(pages,MallProductDescriptionLanguageResponse.class);
        return ReturnResult.success(getInternational(language, InternationalCode.QUERY_SUCCESS), pages);
    }

    @Override
    public ReturnResult<MallProductDescriptionLanguageResponse> detail(Long id, String language) {
        MallProductDescriptionLanguage old = mallProductDescriptionLanguageService.find(id);
        if(null == old){
            return ReturnResult.fail(getInternational(language, InternationalCode.NOT_EXIST));
        }
        MallProductDescriptionLanguageResponse response = BeanUtil.copyProperties(old, MallProductDescriptionLanguageResponse.class);
        return ReturnResult.success(getInternational(language, InternationalCode.QUERY_SUCCESS), response);
    }
}
